//定义一个全局的对象
window.llgd = {};
llgd.transitionEnd = function(dom,callback){
	if (dom && typeof dom=="object") {
		dom.addEventListener('webkitTransitionEnd', function(){
			callback && callback();
		});
		dom.addEventListener('transitionEnd',function(){
			callback && callback();
		})
	}
}
llgd.liTouch = function(dom,callback){
	if(dom && typeof dom =="object"){
		var isMove = false;
		var startTime = 0;
		var startX = 0;
		var endX = 0;

		dom.addEventListener('touchstart', function(e){
			startTime = Date.now();
			if(e.touches.length>1){
				return;
			}
		})
		dom.addEventListener('touchmove',function(e){
			isMove=true;
		})
		dom.addEventListener('touchend',function(e){
			if (!isMove && (Date.now()-startTime) <150) {
				callback&&callback(e);
			}
			startTime = 0;
			isMove =false;
		})
	}
	
}
llgd.fouce_x = function (selector,x){
	selector.on('focus',function(){
		x.removeClass('hidden').addClass('show');
	})
	selector.on('blur',function(){
		x.removeClass('show').addClass('hidden');
	})
}
llgd.className = function(selector,removeclass,addclass){
	selector.removeClass(removeclass).addClass(addclass);
}
llgd.sendCode = function (url,callback,callback2){
	if(window.flag == true)return;
	window.flag = true;
	var phoneNumber=$('#phoneNumber').val();
	if (phoneNumber == "" || phoneNumber.length != 11) {
		alert("请输入正确的手机号");
		return;
	}
	var index = 10;
	$('.bg').show();
	var timer = setInterval(function(){
		index--;
		if (index<0) {
			clearInterval(timer);			
			$('.bg').hide();
			return;
		}
		$('#secondsNum').html('('+index+')');
		if (index==0) {
			window.flag = false;			
			$('#btn-vcode').html('重新获取').on('tap',function(){
				callback && callback();
			})
			return;
		}
	}, 1000)
	$.ajax({
		url: url,
		type: 'post',
		dataType: 'json',
		data: {number: phoneNumber},
		success:function(data){
			console.log(data)
			if (data != 1) {
				$('#code').off('keypress').on('keypress',function (e) {
					if(e.keyCode==13){
						var code = $('#code').val();
						$('#x4').removeClass('show').addClass('hidden');
						$.ajax({
							url: '/index/user/vcode',
							type: 'post',
							dataType: 'json',
							data: {code: code},
							success:function(msg){
								if (msg.code == 2) {
									mui.toast('成功')
									$('#rightcode').toggle('hidden show');
									
									$('#code').val("");
									$('#btn').removeAttr('disabled');
									setTimeout(() => {
										$('#rightcode').hide();
									}, 2000);
									callback2 && callback2();							
								}else{
									$('#errorcode').toggle('hidden show');
									$('#code').val("");
									mui.toast(msg.message);
									setTimeout(() => {
										$('#errorcode').hide();
									}, 2000);
									$('#btn').on('tap',function(){
										return false;
									})
									return;
								}
							}
						})
					}
				})
			}
		}
	})
}

llgd.xClear = function(){
	llgd.fouce_x($('input[name=username]'),$('#x1'));
	llgd.fouce_x($('input[name=password]'),$('#x2'));
	llgd.fouce_x($('input[name=phoneNumber]'),$('#x3'));
	llgd.fouce_x($('input[name=code]'),$('#x4'));
}
llgd.changePage = function(callback){
	$.get("/index/user/checkLogin",
		function (data) {
			if (data.code == 2) {
				callback && callback();
			}else{
				location.href = '/index/user/login.html';
			}
		},
		"json"
	);
}